

package com.bootdo.common.oss;

import cn.hutool.core.util.StrUtil;
import com.bootdo.common.oss.bean.Aliyun;
import com.bootdo.common.oss.bean.Qiniu;
import com.bootdo.program.wallpaper.domain.BzConfigDO;
import com.bootdo.program.wallpaper.service.BzConfigService;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;

@Component
public class ImgJsonSerializer extends JsonSerializer<String> {

    @Resource
    private HttpServletRequest request;

    @Autowired
    private Qiniu qiniu;

    @Autowired
    private Aliyun aliyun;

    @Autowired
    private BzConfigService bzConfigService;


    @Override
    public void serialize(String value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
        if (StrUtil.isBlank(value)) {
            gen.writeString(StrUtil.EMPTY);
            return;
        }
        String[] imgs = value.split(StrUtil.COMMA);
        StringBuilder sb = new StringBuilder();
        List<BzConfigDO> oss_type = bzConfigService.getType("api_oss_type");
        String resourcesUrl = oss_type.get(0).getValue();

        for (String img : imgs) {
            sb.append(resourcesUrl).append(img).append(StrUtil.COMMA);
        }
        sb.deleteCharAt(sb.length() - 1);
        gen.writeString(sb.toString());
    }
}
